import torch
import numpy

# 生成[0, 1)的随机数tensor
x = torch.rand(5, 3)
# 初始的tensor
x = torch.empty(5, 3)
# 全为0的long型tensor
x = torch.zeros(5, 3, dtype=torch.long)
# 从list中初始化tensor
x = torch.tensor([5.5, 3])
# 用原tensor的dtype和device来构建全为1的tensor
x = x.new_ones((5, 3), dtype=torch.double)
# 用标准正态分布来初始化tensor
x = torch.randn(5, 3)
print(x)

y = torch.rand(5, 3)
# tensor相加
print(x + y)
print(torch.add(x, y))
print(x[:, 1])

# tensor转numpy
np = x.numpy()
print(np)
# numpy 转tensor
a = numpy.ones(5)
b = torch.from_numpy(a)
print(b)

# 是否支持CUDA
cuda = torch.cuda.is_available()
print(cuda)
if cuda:
    device = torch.device("cuda")
    cuda_data = torch.ones_like(x, device=device)
    print(cuda_data)
